<?php

/**
 * Description of ReservationGuestForm
\* www.teuker.nl - mail@teuker.nl
 * 
 * @author francisc
 */

class ReservationGuestForm extends sfForm {

    public function configure() {
        $this->setWidgets(array(
            'date' => new sfWidgetFormInput(),
            'occasion' => new sfWidgetFormInput(),
            'time_hour' => new sfWidgetFormSelect(array('choices' => Valuelist::getTimeHourChoices())),
            'time_minute' => new sfWidgetFormSelect(array('choices' => Valuelist::getTimeMinuteChoices())),
            'person_no' => new sfWidgetFormInput(),
            'salutation' => new sfWidgetFormSelectRadio(array('choices' => Valuelist::getGenderChoises(), 'formatter' => 'formatter_radio')),
            'name_first' => new sfWidgetFormInput(),
            'name_last' => new sfWidgetFormInput(),
            'name_company' => new sfWidgetFormInput(),
            'address' => new sfWidgetFormTextarea(),
            'phone' => new sfWidgetFormInput(),
            'mobile' => new sfWidgetFormInput(),
            'email' => new sfWidgetFormInput(),
            'menu_id' => new sfWidgetFormSelectRadio(array('choices' => Valuelist::getMenuChoices(), 'formatter' => 'formatter_radio2')),
            'comments_diet' => new sfWidgetFormTextarea(),
            'comments_general' => new sfWidgetFormTextarea()
        ));

        $this->setValidators(array(
            'date' => new sfValidatorDate(array('required' => true)),
            'occasion' => new sfValidatorString(array('max_length' => 500, 'required' => false)),
            'time_hour' => new sfValidatorString(array('max_length' => 20, 'required' => true)),
            'time_minute' => new sfValidatorString(array('max_length' => 20, 'required' => true)),
            'person_no' => new sfValidatorInteger(array('min' => 1, 'max' => 50,  'required' => true)),
            'salutation' => new sfValidatorInteger(array('min' => 0, 'max' => 1, 'required' => true)),
            'name_last' => new sfValidatorString(array('max_length' => 50, 'required' => true)),
            'name_first' => new sfValidatorString(array('max_length' => 50, 'required' => false)),
            'name_company' => new sfValidatorString(array('max_length' => 50, 'required' => false)),
            'address' => new sfValidatorString(array('max_length' => 500, 'required' => false)),
            'phone' => new sfValidatorString(array('max_length' => 50, 'required' => true)),
            'mobile' => new sfValidatorString(array('max_length' => 50, 'required' => false)),
            'email' => new sfValidatorEmail(array('max_length' => 250, 'required' => true)),
            'menu_id' => new sfValidatorInteger(array('min' => 0, 'required' => false)),
            'comments_diet' => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
            'comments_general' => new sfValidatorString(array('max_length' => 1000, 'required' => false))
        ));
        
        $this->validatorSchema->setPostValidator(
                    new sfValidatorCallback(array('callback' => array($this, 'validateGroupReservation'))));
        
        $this->widgetSchema->setNameFormat('reservation_guest[%s]');
    }
    

    public function validateGroupReservation($validator, $values) {
    	Util::loadHelper('I18N');
        if (Util::is_group_reservation($values['person_no']))
            if (empty($values['menu_id'])) {
                $error = new sfValidatorError($validator, _('With this number of people enter a group menu.'));

                throw new sfValidatorErrorSchema($validator, array('menu_id' => $error));
            }

        return $values;
    }
}
